<?php

class CountWeight
{
    private $apiurl;
    private $apis;
    private $action;

    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'getCountWeightList', 'getCountWeight', 'editCountWeight'
        );
    }

    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;

        $this->action = $action;
        $req = request();
        $req['type'] = $req['type'] ? $req['type'] : 'diary';
        $result = $this->$action($req);
        return $result;
    }

    private function getCountWeightList ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        $req['id'] = 0;
        if ($res) $req['id'] = $res[count($res) -1]['id'];

        $pageUrl = $this->pageUrl($req);
        $type = array( $req['type'] => 'selected', 'type' => $req['type'] );

        return render('count_weight', array('list' => $res, 'pageUrl' => $pageUrl, 'id' => $req['id'], 'type' => $type));
    }

    private function getCountWeight ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res) || empty($res['type_id'])) return false;

        $res['type_desc'] = $res['type'] == 2 ? 'diary' : 'topic';
        return render('weightmodal', $res);
    }

    private function editCountWeight ($req) {
        $param = [
            'do'        => 'setCountWeight',
            'format'    => 'json',
            'type'      => $req['type'],
            'type_id'   => intval($req['type_id']),
            'n1'      => intval($req['n1'])
        ];

        $res = requestApi($param);
        echo $res;
    }

    /**
     * 过滤传参
     * @param unknown $req
     * @return multitype:string unknown Ambigous <number, unknown> Ambigous <string, unknown> NULL number
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'getCountWeightList':
                $param = array(
                    'do'        => 'getCountWeightList',
                    'format'    => 'json',
                    'type'     => $req['type'],
                    'id'        => intval($req['id'])
                );
                break;
            case 'getCountWeight':
                $param = array(
                    'do'        => 'getCountWeight',
                    'format'    => 'json',
                    'type'     => $req['type'],
                    'type_id'   => intval($req['type_id'])
                );
                break;
        }

        return $param;
    }
    

    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "getCountWeightList":
                $mpurl .= "&id={$req['id']}";
                break;
        }

        $pageurl = multi(0, 20, $mpurl);
        return $pageurl;
    }
}

$req = request();
$req['type'] = $req['type'] ? $req['type'] : 'diary';

$diaryTopic   = new CountWeight($_SC ['apiurl']);
echo $diaryTopic->execute($req['op']);
